package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.AgentShopBusEntity;
import io.renren.modules.sys.service.AgentShopBusService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 代理商业务员
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-10 14:17:19
 */
@RestController
@RequestMapping("sys/agentshopbus")
public class AgentShopBusController {
    @Autowired
    private AgentShopBusService agentShopBusService;


    @Autowired
    private SysUserService sysUserService;

    private Map result = new HashMap();

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:agentshopbus:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = agentShopBusService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/saleslist")
    @RequiresPermissions("sys:agentshopbus:saleslist")
    public R saleslist(@RequestParam Map<String, Object> params){
        PageUtils page = sysUserService.queryPageSales(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:agentshopbus:info")
    public R info(@PathVariable("id") Integer id){
        AgentShopBusEntity agentShopBus = agentShopBusService.getById(id);

        return R.ok().put("agentShopBus", agentShopBus);
    }

    /**
     * 信息
     */
    @RequestMapping("/infobus/{id}")
    public R infobus(@PathVariable("id") String id){
        QueryWrapper<AgentShopBusEntity> Wrapper = new QueryWrapper<>();
        AgentShopBusEntity entity = new AgentShopBusEntity();
        if (StringUtils.isNotBlank(id)) {
            Wrapper.eq(StringUtils.isNotBlank(id), "id", id);
            entity = agentShopBusService.getOne(Wrapper);
            return R.ok().put("agentShopBus",entity);
        }
        entity.setBusName("");
        return R.ok().put("agentShopBus",entity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:agentshopbus:save")
    public R save(@RequestBody AgentShopBusEntity agentShopBus){
        agentShopBusService.save(agentShopBus);

        return R.ok();
    }

    @RequestMapping("getUsers")
    public R getUsers(@RequestParam Map<String, Object> params) {
        result.clear();
        PageUtils page = agentShopBusService.queryUsers(params);
        List userList = page.getList();
        result.put("data", userList);
        return R.ok(result);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:agentshopbus:update")
    public R update(@RequestBody AgentShopBusEntity agentShopBus){
        ValidatorUtils.validateEntity(agentShopBus);
        agentShopBusService.updateById(agentShopBus);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:agentshopbus:delete")
    public R delete(@RequestBody Integer[] ids){
        agentShopBusService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestBody Integer id){
        agentShopBusService.removeById(id);

        return R.ok();
    }

    /**
     * 业务员
     */
    @RequestMapping("getBuss")
    public R getBuss() {
        QueryWrapper<AgentShopBusEntity> Wrapper = new QueryWrapper<>();
        AgentShopBusEntity entity = new AgentShopBusEntity();
        entity.setId(999999999);
        entity.setBusName("");
        entity.setBusSn("");
        Wrapper.orderByAsc("id");
        List<AgentShopBusEntity> list = agentShopBusService.list(Wrapper);
        list.add(0,entity);
        return R.ok().put("list",list);
    }
}
